<?php
// check if form was submitted, or just something we show for the first time
  if (isset($_POST['firstName'])){
    // form was submitted, getting information
	$firstName = $_POST['firstName'];
	$lastName = $_POST['lastName'];
	$phone = $_POST['phone'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zipCode = $_POST['zipCode'];
	$email = $_POST['email'];
	$confirmEmail = $_POST['confirmEmail'];
	$university = $_POST['university'];
	$password = $_POST['password'];
	$confirmPassword = $_POST['confirmPassword'];
	$termsAgree = $_POST['termsAccept'];
	
	date_default_timezone_set('EST');
  	$dateRegistered = date('m/d/Y h:i:s A');
  
	// connection settings stored in file
	include("connectionParameters.php");
	
	//$tknUserName = "Username was taken!";
	//$query = "SELECT email FROM Users";
    //   $userResult = mysql_query($cxn,$query);
    //    if ($userName == $userResult){
    //       echo $tknUserName,die();
    //   }
		
   $connection = mysql_connect($host,$user,$pass)
		or die ("Can't connect to server, try again later, please");

	mysql_select_db($database);
	
	
	$query = 'insert into `Users`'
		.'(`firstName`,`lastName`,`phone`,`address`,`city`,`state`,`zipCode`,`university`,`email`,`password`,`agreedToTerms`,`dateRegistered`)'
		.' values ('
			. '"' . $firstName . '","' . $lastName . '","' . $phone . '",'
			. '"' . $address . '","' . $city . '","' . $state . '","' . $zipCode . '",'
			. '"' . $university . '","' . $email . '",MD5("' . $password . '"),"'.$termsAgree.'","'.$dateRegistered.'"'
		.')';
	$result = mysql_query($query,$connection)
		or die ("Problem with adding information, please try again later. SQL");
	
	// info was added
	$query = "select UserId from Users where email='".$email."' and password='".$password."';";
	$result = mysql_query($query,$connection)
		or die ("Problem with adding information, please try again later. Email");
	
	$row = mysql_fetch_row($result);
	$userId = $row[0];

	$userCode = md5($email." ".$password);

	$query = "insert into `Activation`(`userId`,`userCode`,`activated`) values ( '".$userId."','".$userCode."','0');";
	$result = mysql_query($query,$connection)
		or die ("Problem with adding information, please try again later. Activate.");

	// sending message for user to activete
	include("sendActivationEmail.php");
	// end activation email

	//go to the confirmation page
	header("Location:confirmation.php"); 
	// end lightbox message
	// This should be a lighbox confirmation page

	echo "Thank you for registering with Getub! <br/>
	<p></p>
	An e-mail with an activation link has been sent to $email. Please check your inbox within 5-10 minutes to activate your account.
	<br/>
	<p></p>
	Thanks!
	<br />";
	
	mysql_close($connection);
  } else {
  	//LAST RESORT ERROR PLACED HERE
	
  }
?>